package com.cdyZuul.ZuulFilter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import entity.Result;
import entity.StatusCode;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ：cdy
 * @date ：Created in 2020/11/13 16:10
 * @description：www.cdyong.cn
 * 发送过滤器，在这里对请求结果进行拦截
 */
@Component
public class PostFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletResponse response = ctx.getResponse();

        if (response.getStatus() == HttpStatus.NOT_FOUND.value()){
            response.setContentType("application/json; charset=utf8");

            String result  =   new Result(false, StatusCode.NOTFOUNDERROR,"请求路径无效:"+ctx.getRequest().getRequestURI()).toString();

            ctx.setResponseBody(result);
        }

        return null;
    }
}
